import axios from "axios";
import storage from '@/utils/storage.js';

function request(url, formData, method) {
    let token = '';
    if (storage.get('token')) {
        token = 'Bearer ' + storage.get('token');
    }
    if (method === "post") {
        return axios({
            method: 'post',
            url: url,
            data: formData,
            headers: {
                'Content-Type': 'application/json',
                'Authorization': token
            },

        })
    }


    return axios({
        method: 'get',
        url: url,
        params: formData,
        headers: {
            'Content-Type': 'application/json',
            'Authorization': token
        }
    })
}

/**
 * 发送get请求
 * @param url
 * @param formData
 * @returns {Promise<AxiosResponse<any>>|*}
 */
export function getRequest(url, formData) {
    return request(url, formData, 'get');
}

/**
 * 发送poster请求
 * @param url
 * @param formData
 * @returns {Promise<AxiosResponse<any>>|*}
 */
export function postRequest(url, formData) {
    return request(url, formData, 'post');
}